﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Entity;


namespace Pixysoft.Framework.XSchEmA.Sql
{
    internal class MySqlBuilder : ASqlBuilder
    {
        private static volatile MySqlBuilder instance;

        private static object syncRoot = new Object();

        public static MySqlBuilder Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new MySqlBuilder();
                    }
                }

                return instance;

            }
        }


        public override string CreateDatabase(string databasename)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.CreateDatabase(databasename));
            builder.Append(" DEFAULT CHARSET SET utf8");
            //builder.Append(" DEFAULT CHARSET SET gbk");
            return builder.ToString();
        }

        public override string CreateTable(DatabaseSchema database, TableSchema table)
        {
            if (table == null)
                return null;

            StringBuilder builder = new StringBuilder();
            builder.Append(base.CreateTable(database, table));
            //builder.Append(" ENGINE=MyISAM DEFAULT CHARSET=gbk"); //09-10-12 不支持外键
            builder.Append(" ENGINE=InnoDB DEFAULT CHARSET=utf8");
            //builder.Append(" ENGINE=InnoDB DEFAULT CHARSET=gbk");
            return builder.ToString();


        }

        public string AlterTableModifyAutoIncColumn(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " MODIFY \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(" NOT NULL AUTO_INCREMENT");
            //builder.Append(";");
            return builder.ToString();
        }

        public override string AlterTableModifyColumn(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " MODIFY \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(CreateDefaultValue(column));
            //builder.Append(";");
            return builder.ToString();
        }

        public override string AlterTableModifyColumnNullable(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " MODIFY \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(CreateStrongNullable(column));
            //builder.Append(";");
            return builder.ToString();
        }

        public override string AlterTableDropConstraint(PrimaryKeySchema pk)
        {
            if (pk == null)
                return null;


            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + pk.TableName + " DROP PRIMARY KEY");
            return builder.ToString();
        }

        public override string AlterTableDropIndex(IndexSchema index)
        {
            if (index == null)
                return null;

            //DROP INDEX {0} ON {1}

            StringBuilder builder = new StringBuilder();

            builder.Append(string.Format("DROP INDEX {0} ON {1}",
                index.ConsName, index.TableName));

            return builder.ToString();

        }
    }
}
